<!DOCTYPE html>
<html lang="zh">
	<head>
		<meta charset="UTF-8">
		<meta name="viewport" content="width=device-width, initial-scale=1.0">
		<meta http-equiv="X-UA-Compatible" content="ie=edge">
		<title></title>
		<style type="text/css">
			html,
			body,
			canvas{
				padding: 0;
				margin: 0;
				width: 100%;
				height: 100%;
				overflow-y: hidden;
				background-color: transparent;
			}
			canvas {
				width: 100%;
				height: 100%;
			}
		</style>
	</head>
	<body>
		<canvas id="lime-painter"></canvas>
		<script type="text/javascript" src="./uni.webview.1.5.2.js"></script>
		<script type="text/javascript" src="./painter.js"></script>
		<script>
			var cache = [];
			var painter = null;
			var canvas = null;
			var context = null;
			var timer = null;
			var pixelRatio = 1;
			console.log = function(...args) {
				postMessage(args);
			};
			function stringify(key, value) {
				if (typeof value === 'object' && value !== null) {
					if (cache.indexOf(value) !== -1) {
						return;
					}
					cache.push(value);
				}
				return value;
			};
			function emit(event, data) {
				let dataStr = typeof data !== 'object' && data !== null ? data : JSON.stringify(data, stringify);
				postMessage({
					event,
					data: dataStr
				});
				cache = [];
			};
			function postMessage(data) {
				uni.postMessage({
					data
				});
			};
			function init() {
				canvas = document.querySelector('#lime-painter');
				context = canvas.getContext('2d');
				pixelRatio = window.devicePixelRatio;
				painter = new Painter({
				      id: 'lime-painter',
				      context,
				      canvas,
				      pixelRatio,
				      width: canvas.offsetWidth,
				      height: canvas.offsetHeight
				});
				emit('inited', true);
				painter.listen('progressChange', (v) => {
					emit('progressChange', v);
				});
			};
			function save(args) {
				delete args.success;
				delete args.fail;
				clearTimeout(timer);
				timer = setTimeout(() => {
					const path = painter.save(args);
					if(typeof path == 'string') {
						const index = Math.ceil(path.length / 8);
						for (var i = 0; i < 8; i++) {
							if(i == 7) {
								emit('success', path.substr(i * index, index));
							} else {
								emit('file', path.substr(i * index, index));
							}
						};
					} else {
						emit('fail', '');
					};
				}, 30);
			};
			async function source(args) {
				let res = await painter.source(args);
				emit('layoutChange', res);
				await painter.render();
			};
			init()
		</script>
	</body>
</html>
